package com.migration.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * JPA配置 - 旧数据库
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "oldEntityManagerFactory",
        transactionManagerRef = "oldTransactionManager",
        basePackages = {"com.migration.model.olddb"}
)
public class JpaConfig {

    /**
     * 旧数据库实体管理器工厂
     */
    @Primary
    @Bean(name = "oldEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean oldEntityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("oldDataSource") DataSource dataSource) {
        
        Map<String, String> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", "none");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.show_sql", "true");
        
        return builder
                .dataSource(dataSource)
                .packages("com.migration.model.olddb")
                .persistenceUnit("oldPU")
                .properties(properties)
                .build();
    }

    /**
     * 新数据库实体管理器工厂
     */
    @Bean(name = "newEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean newEntityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("newDataSource") DataSource dataSource) {
        
        Map<String, String> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", "none");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.show_sql", "true");
        
        return builder
                .dataSource(dataSource)
                .packages("com.migration.model.newdb")
                .persistenceUnit("newPU")
                .properties(properties)
                .build();
    }

    /**
     * 旧数据库事务管理器
     */
    @Primary
    @Bean(name = "oldTransactionManager")
    public PlatformTransactionManager oldTransactionManager(
            @Qualifier("oldEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

    /**
     * 新数据库事务管理器
     */
    @Bean(name = "newTransactionManager")
    public PlatformTransactionManager newTransactionManager(
            @Qualifier("newEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
} 